package com.felix.netty;


import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * BasicChannelExample.java
 *
 * @author xuejinhua
 * @date 2018/10/17 9:45
 */
public class BasicChannelExample {

    public static void read(String filename) throws IOException {
        RandomAccessFile accessFile = new RandomAccessFile(filename, "rw");
        FileChannel inChannel = accessFile.getChannel();
        ByteBuffer buffer = ByteBuffer.allocate(48);
        int bytesRead = inChannel.read(buffer);
        while (bytesRead != -1) {
            System.out.println("Read " + bytesRead);
            buffer.flip();
            while (buffer.hasRemaining()) {
                System.out.print(buffer.get());
            }
            buffer.clear();
            bytesRead = inChannel.read(buffer);
        }
        accessFile.close();
    }

    public static void main(String[] args) throws IOException {
        read("C:\\Users\\xuejinhua\\Desktop\\account.txt");
    }
}
